﻿Imports System.Activities
Imports System.Data.SqlClient
Imports System.Activities.Presentation.Metadata
Imports System.ComponentModel
Imports System.Drawing

Public Class VerifyIntergirtyActivity
    Inherits MSSQLBaseActivity

    Public Sub New()
        Me.DisplayName = My.Resources.VerifyIntergirtyActivityDisplayName
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim conn = context.GetValue(Me.ConnectionStrings)
        Dim useLog = context.GetValue(Me.OutSQLLog)
        Dim asyncExecute = New asyncExecuteDelegate(AddressOf ExecuteSQL)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(CreateSQLStrings(), conn, True, useLog, callback, state)
    End Function

    Public Overrides Sub Register()
        MyBase.Register()

        Dim build As New AttributeTableBuilder
        'ExecuteApplicationActivity Activity
        build.AddCustomAttributes(GetType(VerifyIntergirtyActivity),
                                              New ToolboxBitmapAttribute(GetType(VerifyIntergirtyActivity), "VerifyIntergirty.png"))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

    Protected Overrides Function ExecuteSQL(script As String, connect As String, needRes As Boolean, ByVal needLog As Boolean) As System.Data.DataSet
        Dim sqlExec As New SQLExecute With {.Timeout = 0}
        sqlExec.ExecuteSQL(script, connect, needRes)

        Return sqlExec.ConvertMessageToDataset(sqlExec.InfoMessages)
    End Function

    ''' <summary>実行する SQL の生成</summary>
    ''' <remarks>http://msdn.microsoft.com/ja-jp/library/ms176064(SQL.90).aspx</remarks>
    Private Function CreateSQLStrings() As String
        Dim sqlBuilder As New System.Text.StringBuilder
        With sqlBuilder
            .Append("DBCC CHECKDB ")
            .Append(";")
        End With
        Return sqlBuilder.ToString
    End Function
End Class
